package com.cts.tmall.biz.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cts.tmall.com.exception.SystemException;
import com.cts.tmall.com.message.RspCode;
import com.cts.tmall.dal.entity.TmallAddr;
import com.cts.tmall.dal.mapper.TmallAddrMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AddrService {

    @Autowired
    private TmallAddrMapper addrMapper;

    public TmallAddr addAddress(TmallAddr addr) {
        if (addr == null) {
            log.error("请求参数为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "请求参数为空！");
        }
        if (StringUtils.isBlank(addr.getConnectName())) {
            log.error("联系人为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "联系人为空！");
        }
        if (StringUtils.isBlank(addr.getConnectTel())) {
            log.error("联系人电话为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "联系人电话为空！");
        }
        if (StringUtils.isBlank(addr.getConnectAddress())) {
            log.error("地址信息为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "地址信息为空！");
        }
        if (StringUtils.isBlank(addr.getConnectAddressDetail())) {
            log.error("详细地址为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "详细地址为空！");
        }

        addr.setSendDefault("02");
        addr.setRefundDefault("02");
        addrMapper.insert(addr);
        return addr;
    }

    public List<TmallAddr> queryAddress(TmallAddr addr) {
        return addrMapper.selectList(Wrappers.<TmallAddr>lambdaQuery().eq(TmallAddr::getCreateUserId, addr.getCreateUserId()));
    }

    public void deleteAddress(Long addrNo) {
        if (addrNo != null) {
            TmallAddr addr = addrMapper.selectById(addrNo);
            if ("01".equals(addr.getSendDefault()) || "01".equals(addr.getRefundDefault())) {
                log.error("默认地址，不能删除！");
                throw new SystemException(RspCode.FAILURE_SYSTEM, "默认地址，不能删除！");
            }
            addrMapper.deleteById(addrNo);
        }
    }

    public TmallAddr updateAddress(TmallAddr addr) {
        if (addr == null) {
            log.error("请求参数为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "请求参数为空！");
        }
        if (addr.getAddrNo() == null) {
            log.error("地址编号为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "地址编号为空！");
        }
        if (StringUtils.isBlank(addr.getConnectName())) {
            log.error("联系人为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "联系人为空！");
        }
        if (StringUtils.isBlank(addr.getConnectTel())) {
            log.error("联系人电话为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "联系人电话为空！");
        }
        if (StringUtils.isBlank(addr.getConnectAddress())) {
            log.error("地址信息为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "地址信息为空！");
        }
        if (StringUtils.isBlank(addr.getConnectAddressDetail())) {
            log.error("详细地址为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "详细地址为空！");
        }

        if (addr.getSendDefault().equals("01")) {
            TmallAddr updAddr = new TmallAddr();
            updAddr.setSendDefault("02");
            addrMapper.update(updAddr, Wrappers.<TmallAddr>lambdaUpdate().eq(TmallAddr::getCreateUserId, addr.getCreateUserId()));
        }

        if (addr.getRefundDefault().equals("01")) {
            TmallAddr updAddr = new TmallAddr();
            updAddr.setRefundDefault("02");
            addrMapper.update(updAddr, Wrappers.<TmallAddr>lambdaUpdate().eq(TmallAddr::getCreateUserId, addr.getCreateUserId()));
        }
        addrMapper.updateById(addr);
        return addrMapper.selectById(addr.getAddrNo());
    }
}
